package com.security.processor;

import com.security.code.ImageCode;
import com.security.code.ValidateCode;
import com.security.config.CodeProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;
import javax.imageio.ImageIO;
import javax.servlet.http.Cookie;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;


/**
 * @author td
 * @date 2017/11/1
 */
@Component("imageValidateCodeProcessor")
public class ImageCodeProcessor extends AbstractValidateCodeProcessor<ValidateCode>{

    @Autowired
    private CodeProperties codeProperties;
    private static Logger logger = LoggerFactory.getLogger(ImageCodeProcessor.class);

    @Override
    protected void send(ServletWebRequest request, ValidateCode validateCode) throws Exception {

        ImageCode imageValidateCode = (ImageCode) validateCodeAddPic(request,validateCode);

        Cookie cookie = new Cookie(codeProperties.getImageCode().getDefaultParameName(),imageValidateCode.getCodeValideId());
        request.getResponse().addCookie(cookie);

        logger.info("【发送图片验证码】发送的图片验证码为: {}",imageValidateCode.getCode());
        ImageIO.write(imageValidateCode.getImage(),"JPEG",request.getResponse().getOutputStream());
    }


    private ValidateCode validateCodeAddPic(ServletWebRequest request, ValidateCode validateCode) {


        int width = ServletRequestUtils.getIntParameter(request.getRequest(), "width",
                codeProperties.getImageCode().getWidth());

        int height = ServletRequestUtils.getIntParameter(request.getRequest(), "height",
                codeProperties.getImageCode().getHeight());
      /*  int width = 67;
        int height = 23;*/
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics g = image.getGraphics();

        Random random = new Random();

        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        g.setFont(new Font("Times New Roman", Font.ITALIC, 20));
        g.setColor(getRandColor(160, 200));
        for (int i = 0; i < 155; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.drawLine(x, y, x + xl, y + yl);
        }


        /// 暂时注释
        for (int i = 0; i < codeProperties.getImageCode().getLength(); i++) {
            String rand = String.valueOf(validateCode.getCode().charAt(i));

            g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
            g.drawString(rand, 13 * i + 6, 16);
        }

        g.dispose();


        return new ImageCode(validateCode.getCodeValideId(),image,validateCode.getCode(),validateCode.getExpireTime());
    }






    /**
     * 生成随机背景条纹
     *
     * @param fc
     * @param bc
     * @return
     */
    private Color getRandColor(int fc, int bc) {
        Random random = new Random();
        if (fc > 255) {
            fc = 255;
        }
        if (bc > 255) {
            bc = 255;
        }
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }

}
